<?php
/**
 * EventService.php
 *
 * @copyright   2023 thingsworld.com.cn - All Rights Reserved
 * @link        http://thingsworld.com.cn/
 * @author      stiffer.chen <chenl@thingsworld.com.cn>
 * @created     2023-03-30 10:44:38
 * @modified    2023-03-30 10:44:38
 *
 */

namespace App\Services;

use App\Models\Event;
use App\Models\WorkOrder;
use App\Repositories\EventRepo;
use Carbon\Carbon;

class EventService extends BaseService
{
    private $telephone;

    public function __construct($telephone = '')
    {
        $this->telephone = $telephone;
    }

    public function process()
    {
        $type = request()->get('type') ?? '';
        $status = request()->get('status') ?? '';
        $perPage = intval(request()->get('limit') ?? 0);
        if ($perPage < 1) {
            $perPage = 10;
        }

        $builder = Event::query()
            ->where('contact_telephone', $this->telephone);

        $builder->with(['product', 'images', 'work_order']);

        if ($type) {
            $builder->where('type', $type);
        }
        if ($status && $status != EventRepo::STATUS_ALL) {
            $builder->where('status', $status);
        }
        $builder->orderByDesc('update_time');
        $data = $builder->paginate($perPage);
        $data->transform(function ($item) {
            $tempItem = $item;
            $product = $tempItem->product;
            $images = $tempItem->images;
            unset($item);

            $detailImages = [];
            foreach ($images as $image) {
                $detailImages[] = (new ImageService($image->image))->getUrl();
            }

            $workOrderId = $tempItem->work_order->id ?? 0;
            $workOrderLogs = [];
            if ($workOrderId) {
                $workOrder = WorkOrder::find($workOrderId);
                if (!empty($workOrder)) {
                    $workOrderLogs = $workOrder->logs ?? [];
                }
            }

            $detail = [
                'customer' => $tempItem->customer_name,
                'work_order_id' => $workOrderId,
                'status' => $tempItem->status,
                'product_number' => $product->product_number ?? $tempItem->product_number ?? '',
                'create_time' => Carbon::parse($tempItem->create_time)->toDateTimeString(),
                'description' => $tempItem->description,
                'images' => $detailImages,
                'event_number' => $tempItem->event_number,
                'contact_name' => $tempItem->contact_name,
                'contact_telephone' => $tempItem->contact_telephone,
                'address' => $tempItem->province_name . $tempItem->city_name . $tempItem->county_name . $tempItem->address,
                'work_order_logs' => $workOrderLogs,
                'event_logs' => $tempItem->logs
            ];

            $event = [
                'id' => $tempItem->id,
                'event_number' => $tempItem->event_number,
                'type' => $tempItem->type,
                'status' => $tempItem->status,
                'create_time' => Carbon::parse($tempItem->create_time)->toDateTimeString(),
                'detail' => $detail
            ];
            //return $item;
            return $event;
        });
        return $data;
    }
}